package controller;

import core.BaseServlet;
import dao.TableDao;
import dao.impl.TableDaoImpl;
import pojo.Table;
import service.TableService;
import util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "TableServlet", urlPatterns = "/table")
public class TableServlet extends BaseServlet {

    private TableService tableService = new TableService();

    /**
     * 餐桌管理页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void tableManagePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/operationManage/tableManage/tableManage.jsp").forward(request, response);
    }

    /**
     * 餐桌添加页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void addPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/operationManage/tableManage/tableAdd.jsp").forward(request, response);
    }

    /**
     * 餐桌添加
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        tableService.add(request, response);
    }

    /**
     * 根据餐桌类型查找餐桌编号
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findTableCodeByType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        tableService.findTableCodeByType(request, response);
    }

    /**
     * 根据条件查找餐桌
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findTableCodeByParams(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        tableService.findTableCodeByParams(request, response);
    }

    /**
     * 软删除
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        tableService.delete(request, response);
    }

    /**
     * 真删除
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void realDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        tableService.realDelete(request, response);
    }

    /**
     * 编辑页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void editTablePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        tableService.editTablePage(request, response);
    }

    /**
     * 编辑
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        tableService.edit(request, response);
    }

    /**
     * 回收站页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void tableManageDeletedPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/operationManage/tableManage/tableManageDeleted.jsp").forward(request, response);
    }

    /**
     * 恢复
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void restore(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       tableService.restore(request, response);
    }

    /**
     * 通过餐桌id查找餐桌
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findTableById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TableDao tableDao = new TableDaoImpl();
        Table table = tableDao.findEditTable(request.getParameter("tableId"));
        Result.response(200, "查找成功！", response ,table);
    }

}
